package com.vue.admin.controller;

import com.github.pagehelper.PageInfo;
import com.vue.admin.entity.SysUser;
import com.vue.admin.entity.SysUserExample;
import com.vue.admin.service.SysUserService;
import com.vue.admin.util.PageHelper2;
import com.vue.admin.util.ResponseState;
import com.vue.admin.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserServieImpl;
    @RequestMapping(value="/select")
    public PageInfo select(@RequestBody Map map)throws Exception{
        SysUserExample example = new SysUserExample();
        if(map.containsKey("account")){
            example.or().andAccountLike((String)map.get("account"));
        }
        if(map.containsKey("name")){
            example.or().andNameLike((String)map.get("name"));
        }
        PageHelper2.startPage(map);
        List<SysUser> list = sysUserServieImpl.selectByCondition(example);
        PageInfo<SysUser> appsPageInfo = new PageInfo<>(list);
        return appsPageInfo;//PageUtil.pageVO();

    }


    @RequestMapping(value="/del")
    public ResponseState del(@RequestBody SysUser sysUser){
        if(StringUtil.isEmpty(sysUser.getId())){
            return new ResponseState(false,"请选择要删除的用户！");
        }
        sysUserServieImpl.delete(sysUser.getId());
        return new ResponseState();
    }
    @RequestMapping(value="/save")
    public ResponseState save(@RequestBody SysUser sysUser){
        if(StringUtil.isEmpty(sysUser.getAccount())){
            return new ResponseState(false,"账号不能为空！");
        }
        sysUserServieImpl.save(sysUser);
        return new ResponseState();
    }
}
